/**
 * 作者：陈文斌 on 2019/1/25.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class AddImageDialog extends Dialog {
  final BuildContext sheetContext;

  /// 选择完成后的回调
  final Function imageFileCallback;

  AddImageDialog({
    Key key,
    @required this.sheetContext,
    this.imageFileCallback,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Material(
        type: MaterialType.transparency,
        child: AddImageContent(
          imageFileCallback: imageFileCallback,
          sheetContext: sheetContext,
        ),
      ),
    );
  }
}

class AddImageContent extends StatefulWidget {
  final BuildContext sheetContext;
  final Function imageFileCallback;

  @override
  AddImageContent({
    Key key,
    this.sheetContext,
    this.imageFileCallback,
  }) : super(key: key);

  State<StatefulWidget> createState() => _AddImageContentState();
}

class _AddImageContentState extends State<AddImageContent> {
  final double _promptH = 36.0;
  final double _buttonH = 50.0;

  final TextStyle _promptTextStyle = TextStyle(
    color: Color(0xFF666666),
    fontSize: 13,
  );

  final TextStyle _buttonTextStyle = TextStyle(
    color: Color(0xFF232323),
    fontSize: 18,
  );

  @override
  Widget build(BuildContext context) {
    final double _screenWidth = MediaQuery.of(context).size.width;

    _onPressed(ImageSource source) {
      print('开始选择图片');
      ImagePicker().pickImage(source: source).then(
        (XFile imageFile) {
          if (imageFile != null) {
            if (widget.imageFileCallback != null) {
              widget.imageFileCallback(imageFile);
            }
//            print('${context},${widget.sheetContext}');
            Navigator.pop(context);
          } else {
            print('未选中图片');
            Navigator.pop(context);
          }
        },
      );
    }

    return SafeArea(
      child: Material(
        child: SizedBox(
          width: _screenWidth,
          height: _promptH + _buttonH * 2 + 1,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                constraints: BoxConstraints(minHeight: _promptH),
                width: _screenWidth,
                color: Color(0xFFF5F5F5),
                alignment: Alignment.center,
                child: Text(
                  '严禁上传、存储、传播黄赌毒以及侵权等违法违规内容',
                  style: _promptTextStyle,
                ),
              ),
              Container(
                height: _buttonH,
                width: _screenWidth,
                color: Colors.white,
                child: MaterialButton(
                  onPressed: () => _onPressed(ImageSource.camera),
                  child: Text(
                    '拍照',
                    style: _buttonTextStyle,
                  ),
                ),
              ),
              Divider(
                height: 1,
                color: Color(0xFFE3E3E3),
              ),
              Container(
                height: _buttonH,
                width: _screenWidth,
                color: Colors.white,
                child: MaterialButton(
                  onPressed: () => _onPressed(ImageSource.gallery),
                  child: Text(
                    '系统相册',
                    style: _buttonTextStyle,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
